home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / share / hal / device-manager / GtkAttributesFacade.py < prev    next >
Encoding:
Python Source  |  2007-05-11  |  2.1 KB  |  64 lines

  1. """This module contains the GtkAttributesFacade class."""
  2.  
  3. from gobject import GObject
  4.  
  5.  
  6. class GtkAttributesFacade:
  7.  
  8.     """Wrap a GTK instance to simplify the way attributes are referenced.
  9.  
  10.     Given a GTK instance i, make it possible for any functions i.get_foo() and
  11.     i.set_foo(value) to be accessed via i.foo like a normal attribute.
  12.  
  13.     The following attributes are used:
  14.  
  15.     instance - This is the GTK instance that is being wrapped.
  16.  
  17.     """
  18.  
  19.     def __init__(self, instance):
  20.         """Accept the instance."""
  21.         self.__dict__["instance"] = instance
  22.  
  23.     def __setattr__(self, name, value):
  24.         """Simplify the way attributes are referenced.
  25.  
  26.         When trying to do self.foo = something, if there is a
  27.         self.instance.set_foo() method, use it.  Otherwise, just set the
  28.         attribute in self.
  29.  
  30.         Return value so that chaining is possible.
  31.         
  32.         """
  33.         setter = "set_" + name
  34.         if hasattr(self.instance, setter):
  35.             apply(getattr(self.instance, setter), [value])
  36.         else:
  37.             self.__dict__[name] = value
  38.         return value
  39.  
  40.     def __getattr__(self, name):
  41.         """Simplify the way attributes are referenced.
  42.  
  43.         Remember that this method is called after a failed lookup in self.  Try
  44.         looking for self.instance.foo.  Next, try looking for a
  45.         self.instance.get_foo() method, and call it if it exists.  Otherwise,
  46.         raise an exception.
  47.  
  48.         If a value is successfully looked up, and if it is a subclass of 
  49.         GObject, wrap it in a GtkAttributesFacade before returning it.
  50.         
  51.         """
  52.         getter = "get_" + name
  53.         if hasattr(self.instance, name):
  54.             ret = getattr(self.instance, name)
  55.         elif hasattr(self.instance, getter):
  56.             ret = apply(getattr(self.instance, getter))
  57.         else:
  58.             raise AttributeError(
  59.                 "%s instance has no attribute '%s'" % 
  60.                     (self.instance.__class__.__name__, name))
  61.         if isinstance(ret, GObject):
  62.             ret = GtkAttributesFacade(ret)
  63.         return ret
  64.